<?php
namespace app\admin\controller;
use app\admin\model\AdModel;
use think\Controller;

// 活动管理
class Am extends Controller{
	// 活动列表
	public function activityList(){

		if(request()->isAjax()){
            $return = db('articles')-> where('cat_id','5') -> order('sold_out,end_time desc ')-> select();
            //判断日期是否过期
           
            foreach ($return as $key => $value) {
            		$time =strtotime($value['end_time']);
            		if($value['sold_out'] == 1){
            		if($time <= time()){
            			$return[$key]['sold_out'] = 2;
            			 $id = db('articles')-> where("id = {$value['id']}") ->update(['sold_out' => 2]);
            		}
            		}
            }
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch('activityList');
		}

	}
	// 新增活动
	public function addActivity(){

		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("thumbnail");

    	// 移动到框架应用根目录/uploads/ 目录下
    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
    		if($info){
		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["thumbnail"] = $route;

		        $result = db('articles')->insert($data);

		        if($result){
		        	$this->success('新增成功', 'addActivity','',2);
		        }
			}
		}

		return $this->fetch('addActivity');
	}
	// 删除活动
	public function removeActivity(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		$result = db('articles')->where('id')->delete($requestData["id"]);

		if($result){
			echo 1;
		}
	}
	// 修改活动
	public function editActivity(){

		$data = input('get.id');

		$formData = db('articles')->where('id',$data)->select();

		$this->assign('formData',$formData);

		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("thumbnail");

    		// 如果有上传图片
    		if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $upData = input('post.');
		        $upData["thumbnail"] = $route;

		        $result = db('articles')->where('id',$upData["id"])->update($upData);

		        if($result){
		        	$this->success('修改成功', 'activityList','',2);
		        }

    		}else {

    			$upData = input('post.');
    			$result = db('articles')->where('id',$upData["id"])->update($upData);
    			if($result){
		        	$this->success('修改成功', 'activityList','',2);
		        }
    		}
    		
		}

		return $this->fetch('editActivity');
	}
	// 查看详情
	public function checkActivity(){
		$data = input('get.id');

		$formData = db('articles')->where('id',$data)->select();

		$this->assign('formData',$formData);

		return $this->fetch('checkActivity');
	}

	// 品牌街铺
	public function brandList(){
		if(request()->isAjax()){
            $return = db('articles')-> where('cat_id','6') -> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch('brandList');
		}
	}
	// 增加
	public function addBrand(){
		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("thumbnail");

    	// 移动到框架应用根目录/uploads/ 目录下
    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
    		if($info){
		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		       //  $image = \think\Image::open(ROOT_PATH . 'upload/'. $info->getSaveName());
		       // var_dump($image);die();
		        
		       
		        $data = input('post.');
		        $data["thumbnail"] = $route;

		        $result = db('articles')->insert($data);

		        if($result){
		        	$this->success('新增成功', 'addBrand','',2);
		        }
			}
		}

		return $this->fetch('addBrand');
	}
	// 删除
	public function removeBrand(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		$result = db('articles')->where('id')->delete($requestData["id"]);

		if($result){
			echo 1;
		}
	}
	// 修改
	public function editBrand(){
		$data = input('get.id');

		$formData = db('articles')->where('id',$data)->select();

		$this->assign('formData',$formData);

		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("thumbnail");

    		// 如果有上传图片
    		if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $upData = input('post.');
		        $upData["thumbnail"] = $route;

		        $result = db('articles')->where('id',$upData["id"])->update($upData);

		        if($result){
		        	$this->success('修改成功', 'brandList','',2);
		        }

    		}else {

    			$upData = input('post.');
  
    			$result = db('articles')->where('id',$upData["id"])->update($upData);

    			if($result){
		        	$this->success('修改成功', 'brandList','',2);
		        }
    		}
    		
		}

		return $this->fetch('editBrand');
	}
	// 查看
	public function checkBrand(){
		$data = input('get.id');
		
		$formData = db('articles')->where('id',$data)->select();

		$this->assign('formData',$formData);

		return $this->fetch('checkBrand');
	}
}